<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java I/O模型深度解析：BIO、NIO与AIO</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            border-color: rgba(102, 126, 234, 0.5);
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            border-bottom: 1px solid #3e3e3e;
            font-size: 14px;
            color: #888;
        }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            color: #d4d4d4;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .comparison-table {
            background: white;
            border-radius: 16px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .comparison-table th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: 600;
            padding: 16px;
            text-align: left;
        }
        
        .comparison-table td {
            padding: 16px;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .comparison-table tr:last-child td {
            border-bottom: none;
        }
        
        .comparison-table tr:hover {
            background: #f9fafb;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
            margin-bottom: 2rem;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Java I/O模型深度解析
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    探索BIO、NIO与AIO的技术奥秘，掌握高性能网络编程的核心
                </p>
                <div class="flex justify-center space-x-8 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-server mr-2"></i>
                        <span>高并发架构</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-bolt mr-2"></i>
                        <span>性能优化</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-code mr-2"></i>
                        <span>实战案例</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>Java编程中，BIO（Blocking I/O）、NIO（Non-blocking I/O）、和AIO（Asynchronous I/O）分别代表不同的I/O模型。这三种模型提供了不同的方式来处理网络通信、文件操作等I/O任务。理解这三种I/O模型的特性及其在Java中的实现，对于开发高效、可靠的应用程序非常重要。
                </p>
            </div>
        </div>
    </section>

    <!-- I/O Models Overview -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="section-title text-3xl font-bold text-gray-800 mb-12">I/O模型架构概览</h2>
                <div class="mermaid">
                    graph TB
                        A[Java I/O模型] --> B[BIO<br/>阻塞式I/O]
                        A --> C[NIO<br/>非阻塞式I/O]
                        A --> D[AIO<br/>异步I/O]
                        
                        B --> B1[同步阻塞]
                        B --> B2[一连接一线程]
                        B --> B3[简单易用]
                        
                        C --> C1[同步非阻塞]
                        C --> C2[多路复用]
                        C --> C3[Channel+Buffer]
                        
                        D --> D1[异步非阻塞]
                        D --> D2[回调机制]
                        D --> D3[事件驱动]
                        
                        style A fill:#667eea,stroke:#764ba2,stroke-width:3px,color:#fff
                        style B fill:#f59e0b,stroke:#d97706,stroke-width:2px,color:#fff
                        style C fill:#10b981,stroke:#059669,stroke-width:2px,color:#fff
                        style D fill:#ef4444,stroke:#dc2626,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>
    </section>

    <!-- BIO Section -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <div class="mb-12">
                    <h2 class="section-title text-4xl font-bold text-gray-800 flex items-center">
                        <i class="fas fa-pause-circle mr-4 feature-icon"></i>
                        BIO（Blocking I/O）
                    </h2>
                </div>
                
                <div class="grid md:grid-cols-2 gap-8 mb-12">
                    <div class="card-hover bg-white rounded-xl p-8 shadow-lg">
                        <h3 class="text-2xl font-semibold mb-4 text-gray-800">
                            <i class="fas fa-info-circle mr-2 text-purple-600"></i>概念解析
                        </h3>
                        <p class="text-gray-600 leading-relaxed">
                            BIO，全称为<strong class="text-purple-600">Blocking I/O</strong>，是一种<strong class="text-purple-600">阻塞式</strong>的I/O模型。在BIO模型中，I/O操作是同步的且会阻塞调用线程，直到操作完成为止。这意味着在一个线程中发起I/O操作后，该线程将被挂起，直到I/O操作完成。
                        </p>
                    </div>
                    
                    <div class="card-hover bg-white rounded-xl p-8 shadow-lg">
                        <h3 class="text-2xl font-semibold mb-4 text-gray-800">
                            <i class="fas fa-cogs mr-2 text-purple-600"></i>核心特性
                        </h3>
                        <ul class="space-y-3 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1 text-purple-500"></i>
                                <span><strong>阻塞式</strong>：每个I/O操作都会阻塞当前线程，直到操作完成。</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1 text-purple-500"></i>
                                <span><strong>同步调用</strong>：I/O操作按顺序执行，调用方需要等待操作完成。</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1 text-purple-500"></i>
                                <span><strong>线程占用</strong>：每个连接需要一个独立的线程来处理I/O操作。</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1 text-purple-500"></i>
                                <span><strong>简单易用</strong>：编程模型简单，容易理解和实现。</span>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <div class="grid md:grid-cols-2 gap-8 mb-12">
                    <div class="bg-green-50 rounded-xl p-8">
                        <h4 class="text-xl font-semibold mb-4 text-green-800">
                            <i class="fas fa-thumbs-up mr-2"></i>优点
                        </h4>
                        <ul class="space-y-2 text-green-700">
                            <li class="flex items-start">
                                <i class="fas fa-plus mr-2 mt-1"></i>
                                <span>编程简单，逻辑清晰，适合小规模应用。</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-plus mr-2 mt-1"></i>
                                <span>每个请求在一个独立的线程中处理，便于隔离和调试。</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="bg-red-50 rounded-xl p-8">
                        <h4 class="text-xl font-semibold mb-4 text-red-800">
                            <i class="fas fa-thumbs-down mr-2"></i>缺点
                        </h4>
                        <ul class="space-y-2 text-red-700">
                            <li class="flex items-start">
                                <i class="fas fa-minus mr-2 mt-1"></i>
                                <span>线程资源消耗大，处理大量并发连接时效率低。</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-minus mr-2 mt-1"></i>
                                <span>线程上下文切换开销大，容易导致性能瓶颈。</span>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <div class="mb-12">
                    <h3 class="text-2xl font-semibold mb-6 text-gray-800">
                        <i class="fas fa-code mr-2 text-purple-600"></i>实现示例
                    </h3>
                    <div class="code-block">
                        <div class="code-header">
                            <i class="fab fa-java mr-2"></i>BIOServer.java
                        </div>
                        <pre><code>import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class BIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("BIO Server started, listening on port 8080");

        while (true) {
            Socket clientSocket = serverSocket.accept(); // 阻塞
            new Thread(new ClientHandler(clientSocket)).start(); // 每个连接一个线程
        }
    }
}

class ClientHandler implements Runnable {
    private Socket clientSocket;

    public ClientHandler(Socket socket) {
        this.clientSocket = socket